package com.haidechizi.fastadmin.common.aspect;

import com.haidechizi.fastadmin.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * redis切面类
 * <p>
 * 可以定义是否开启redis缓存
 */
@Slf4j
@Aspect
@Component
public class RedisAspect {
    @Value("${cache.redis.open: false}")
    private boolean open;

    public RedisAspect() {
    }

    @Around("execution(* com.haidechizi.fastadmin.common.utils.RedisUtils.*(..))")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object result = null;
        if (this.open) {
            try {
                result = point.proceed();
            } catch (Exception e) {
                log.error("redis error", e);
                throw new BusinessException("Redis服务异常");
            }
        }
        return result;
    }
}
